<template>
  <div>
    <van-nav-bar
      title="道具卡"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="card-container">
      <div v-for="card in cards" :key="card.id" class="card">
        <img :src="card.image || defaultImage" alt="道具卡图片" class="card-image" />
        <div class="card-info">
          <div v-if="card.type" class="card-type">{{ card.type }}</div>
          <div v-if="card.description" class="card-description">{{ card.description }}</div>
          <div v-if="card.usage" class="card-footer">
            <div class="card-usage">{{ card.usage }}</div>
            <div class="card-count" v-if="card.count !== undefined">次数: {{ card.count }}</div>
          </div>
          <div v-else class="card-footer center-align">
            <div class="card-more-skills">{{ card.moreSkills || defaultMoreSkills }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import axios from 'axios';

const onClickLeft = () => {
  history.back();
};

const defaultImage = 'https://img0.baidu.com/it/u=130538280,380142463&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1723309200&t=054a332b59e2ba85af02ee05036f1b09';
const defaultMoreSkills = '更多技能，敬请期待';

const cards = ref([]);

const fetchCards = async () => {
  try {
    const response = await axios.get('http://localhost:3789/prop');
    console.log('Fetched cards:', response.data); // 调试信息
    cards.value = response.data.data; // 提取 data 部分

    // 添加默认卡片
    cards.value.push({
      id: 'default',
      image: defaultImage,
      moreSkills: defaultMoreSkills,
    });
  } catch (error) {
    console.error('Failed to fetch cards:', error);
  }
};

onMounted(() => {
  fetchCards();
});
</script>

<style scoped>
.card-container {
  padding: 20px;
}

.card {
  display: flex;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}

.card-image {
  width: 100px;
  height: 100px;
  border-radius: 8px;
  object-fit: cover;
  margin-right: 20px;
}

.card-info {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  flex: 1;
}

.card-type {
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.card-description {
  font-size: 14px;
  color: #888;
  margin: 10px 0;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.center-align {
  align-items: center;
}

.card-usage {
  font-size: 14px;
  color: #e53e3e;
}

.card-count {
  font-size: 14px;
  color: #333;
}

.card-more-skills {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  padding-top: 40px;
}
</style>
